package com.ykdl.member.kid.util;

import java.io.ByteArrayOutputStream;
import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;

import com.tencent.mm.sdk.openapi.IWXAPI;
//import com.tencent.mm.sdk.openapi.SendMessageToWX;
//import com.tencent.mm.sdk.openapi.WXAPIFactory;
//import com.tencent.mm.sdk.openapi.WXImageObject;
//import com.tencent.mm.sdk.openapi.WXMediaMessage;
//import com.tencent.mm.sdk.openapi.WXTextObject;
//import com.tencent.mm.sdk.openapi.WXWebpageObject;
import com.ykdl.member.constant.KidConfig;
/**
 * 微信 工具类
 * @see
 * @since 1.0
 */
public class WeiXinUtil {
	private static final String APP_ID = "wx15d09a66af843997";

	// used by
	// int supportAPI = api.getWXAppSupportAPI();
	// if(supportAPI != 0){
	// WeiXinUtil wxUtil = new WeiXinUtil(mContext, api);
	// wxUtil.shareWX(content, path);
	// }else{
	// Toast.makeText(mContext, "未安装微信！", 1).show();
	// }
//
//	public static IWXAPI getWXAPIFactoryAndReg(Context pcontext) {
//		IWXAPI api = WXAPIFactory.createWXAPI(pcontext, APP_ID);
//		api.registerApp(APP_ID);
//		return api;
//	}
//
//	private IWXAPI mapi;
//	private int THUMB_SIZE=100;
//	public WeiXinUtil(Context context, IWXAPI api) {
//		mapi = api;
//	}

	/*public void shareWXImage(String content, String path) {
		if (path.endsWith(".jpg")) {
			path = path.replace(".jpg", "s.jpg");
		}*/
		// if(content != null){
		// WXTextObject wtobjext = new WXTextObject();
		// wtobjext.text = content;
		//
		// WXMediaMessage wxmsg = new WXMediaMessage();
		// wxmsg.mediaObject = wtobjext;
		// wxmsg.description = content;
		//
		// SendMessageToWX.Req req = new SendMessageToWX.Req();
		// req.transaction = buildTransaction("text");
		// req.message = wxmsg;
		//
		// mapi.sendReq(req);
		// }
	/*	
		WXMediaMessage msg = new WXMediaMessage();
		msg.title = "分享给你个照片";			
		msg.description = content;
		
		WXTextObject textObj = new WXTextObject();
		textObj.text = content;
		msg.mediaObject=textObj;
		
		if (path != null) {
			File file = new File(path);			
			if (file.exists()) {
				WXImageObject imgObj = new WXImageObject();
				msg.mediaObject = imgObj;
				imgObj.setImagePath(path);
				
				Bitmap bmp = BitmapFactory.decodeFile(path);
				Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
				bmp.recycle();
				msg.thumbData = bmpToByteArray(thumbBmp, true);
			}
			
		}
			SendMessageToWX.Req req = new SendMessageToWX.Req();
			req.transaction = buildTransaction("img");
			req.message = msg;
			mapi.sendReq(req);
		
		
		
	}

	public void shareWXWeb(String title,String description, String url,String imgpath) {
		if(description.length()>50){
			description=description.substring(0, 50);
		}
		
		if(title.length()>10){
			title=title.substring(0,10);
		}
		
		WXWebpageObject webpage = new WXWebpageObject();
		webpage.webpageUrl = url+"?mode=1";
		WXMediaMessage msg = new WXMediaMessage(webpage);
		msg.title = title;
		msg.description = description;
		if(imgpath.length()>0){
		imgpath=Environment.getExternalStorageDirectory()+KidConfig.BITMAP_CACHE_PATH+imgpath;
		File f=new File(imgpath);
		if(f.exists()){
		Bitmap bmp = BitmapFactory.decodeFile(imgpath);
		Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
		bmp.recycle();	
		msg.thumbData = bmpToByteArray(thumbBmp, true);
		}
		}
		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("webpage");
		req.message = msg;
		req.scene =  SendMessageToWX.Req.WXSceneTimeline;
		mapi.sendReq(req);
	}
	
	
	public void shareWXWebToGroup(String title,String description, String url,String imgpath,boolean togroup) {
		if(description.length()>50){
			description=description.substring(0, 50);
		}
		
		if(title.length()>10){
			title=title.substring(0,10);
		}
		
		WXWebpageObject webpage = new WXWebpageObject();
		webpage.webpageUrl = url+"?mode=1";
		WXMediaMessage msg = new WXMediaMessage(webpage);
		msg.title = title;
		msg.description = description;
		//imgpath=imgpath;
		if (imgpath!=null) {
			File f=new File(imgpath);
			if(f.exists()){
			Bitmap bmp = BitmapFactory.decodeFile(imgpath);
			Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
			bmp.recycle();	
			msg.thumbData = bmpToByteArray(thumbBmp, true);
			}
		}
		
		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("webpage");
		req.message = msg;
		req.scene = togroup ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
		mapi.sendReq(req);
	}
	
	public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		bmp.compress(CompressFormat.PNG, 100, output);
		if (needRecycle) {
			bmp.recycle();
		}
		
		byte[] result = output.toByteArray();
		try {
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public static byte[] bmpToByteArray1(final Bitmap bmp,
			final boolean needRecycle) {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		bmp.compress(CompressFormat.PNG, 100, output);
		if (needRecycle) {
			bmp.recycle();
		}

		byte[] result = output.toByteArray();
		try {
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}
	private String buildTransaction(final String type) {
		return (type == null)
				? String.valueOf(System.currentTimeMillis())
				: type + System.currentTimeMillis();
	}*/
}
